﻿using CarterWechatServer.BizModel.Order;
using CarterWechatServer.DataEntity;
using CarterWechatServer.DataEntity.Entities;
using CarterWechatServer.Util.StringHelper;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CarterWechatServer.BizLogic.Order
{
    /// <summary>
    /// 订单业务逻辑
    /// </summary>
    public class OrderService
    {
        #region 订单管理==提交订单

        /// <summary>
        /// 订单提交
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public int OrderSubmit(OrderSubmitInput input)
        {
            using(EFContext db=new EFContext())
            {
                List<OrderInfoEntity> orderInfos = new List<OrderInfoEntity>();

                foreach (var cart in input.CartList)
                {
                    DishEntity dish = db.Dishes.Find(cart.DishId);

                    OrderInfoEntity orderInfo = new OrderInfoEntity()
                    {
                        DishId = cart.DishId,
                        Count = cart.Count,
                        Amount = dish.UnitPrice * cart.Count
                    };

                    orderInfos.Add(orderInfo);
                }

                OrderEntity order = new OrderEntity()
                {
                    OrderCode = StringHelper.GenerateOutTradeNo(),
                    OrderTime = DateTime.Now,
                    UserId = input.UserId.Value,
                    OrderStatus = 1,
                    TotalCount = orderInfos.Sum(m => m.Count),
                    TotalAmount = orderInfos.Sum(m => m.Amount),
                    OrderInfo = orderInfos
                };

                db.Orders.Add(order);
                db.SaveChanges();

                return order.Id;
            }
        }


        #endregion

        #region 订单管理==订单详情
        /// <summary>
        /// 订单详情
        /// </summary>
        /// <param name="id">订单id</param>
        /// <returns></returns>
         public OrderDetailOutput OrderDetail(int id)
        {
            using(EFContext db = new EFContext())
            {
                OrderDetailOutput output = db.Orders
                    .Include(m => m.OrderInfo)
                    .Where(m => m.Id == id)
                    .Select(m => new OrderDetailOutput()
                    {
                        OrderStatus = m.OrderStatus,
                        TakeNo = m.TakeNo,
                        WechatOrderCode = m.WechatOrderCode,
                        OrderTime = m.OrderTime,
                        TotalCount = m.TotalCount,
                        TotalAmount = m.TotalAmount,
                        OrderCode = m.OrderCode,
                        OrderInfo = m.OrderInfo
                                .Select(n => new OrderDetailInfo()
                                {
                                    DishName = n.Dish.DishName,
                                    ImageUrl = n.Dish.ImageUrl,
                                    Amount = n.Amount,
                                    Count = n.Count
                                }).ToList()
                    }).FirstOrDefault();
                return output;
            }
        }
        #endregion

        #region 订单管理==订单支付

        /// <summary>
        /// 订单支付
        /// </summary>
        /// <param name="input"></param>
        public void OrderPay(OrderPayInput input)
        {
            using(EFContext db = new EFContext())
            {
                OrderEntity order = db.Orders
                    .FirstOrDefault(m => m.OrderCode == input.OutTradeNo);

                if (order!=null&&order.OrderStatus==1)
                {
                    DateTime today = DateTime.Today;
                    DateTime tomorrow = DateTime.Today.AddDays(1);

                    int totalCount = db.Orders.Count(m => m.OrderTime >= today && m.OrderTime < tomorrow);

                    order.OrderStatus = 2;
                    order.WechatOrderCode = input.TransactionId;
                    order.TakeNo = totalCount + 1;

                    db.SaveChanges();
                }
            }
        }

        #endregion

        #region 订单管理==订单列表
        /// <summary>
        /// 订单列表
        /// </summary>
        /// <returns></returns>
        public List<OrderListOutput> OrderList()
        {
            using(EFContext db=new EFContext())
            {
                List<OrderListOutput> outputs = db.Orders
                    .Select(m => new OrderListOutput()
                    {
                        OrderId = m.Id,
                        TotalAmount = m.TotalAmount,
                        TotalCount = m.TotalCount,
                        OrderStatus = m.OrderStatus,
                        OrderTime = m.OrderTime
                    }).ToList();

                return outputs;
            }
        }
        #endregion
    }
}
